#!/usr/bin/env node

const fs = require('fs')
const path = require('path')
const stat = fs.stat;
const inquirer = require('inquirer')
const ejs = require('ejs')


function copy(src, dest) {
    fs.readdir(src, (err, files) => {
        if (err) throw err
        files.forEach(file => {
            let _src = src + '/' + file
            let _dest = dest + '/' + file
            stat(_src, (err, stats) => {
                if (err) throw err
                if (stats.isFile()) {
                    let readStream = fs.createReadStream(_src)
                    let writeStream = fs.createWriteStream(_dest)
                    readStream.pipe(writeStream)
                } else if (stats.isDirectory()) {
                    fs.exists(_dest, exists => {
                        if(exists) {
                            copy(_src, _dest)
                        } else {
                            fs.mkdir(_dest, () => {
                                copy(_src, _dest)
                            })
                        }
                    })
                }
            })
        })
    })
}

// 命令行交互
inquirer.prompt([
    {
        type: 'input',
        name: 'name',
        message: 'Project name?'
    }
])
.then(anwsers => {
    // 模版文件夹
    const tmplDir = path.join(__dirname, 'templates')
    // 输入文件目录，当前命令行执行的目录
    const destDir = process.cwd()
    // const destDir = path.join('./', 'dist')
    copy(tmplDir, destDir)
})